﻿//
"use strict"
const dbase = require("../dbase");
class item extends dbase.gzone.mongodb {
    constructor(sid, uid) {
        super(sid, 'item');
        this.uid = uid;
        this.ObjectID = ObjectID.bind(this);
    }
    uget(id) {
        let query = this.query(id), fields = arguments[1] || [];
        query["uid"] = this.uid;
        let option = { "multi": true, "fields": this.fields(fields) };
        return this.find(query, option);
    }
    //获取玩家道具列表
    mget(bag) {
        let query = { "uid": this.uid };
        let option = { "multi": true, "dataType": "array", "limit": 3000, 'batchSize': 3000 };
        if (bag) {
            query["bag"] = bag;
        }
        return this.find(query, option);
    }
    //获取玩家道具列表
    mdel(bag) {
        let query = { "endTime": { "$lte": Date.now() } };
        let option = { "multi": true, "dataType": "array", "limit": 3000, 'batchSize': 3000 };
        if (bag) {
            query["bag"] = bag;
        }
        return this.remove(query, option);
    }

    //获取玩家道具数量
    mcount(bag) {
        let query = { "uid": this.uid };
        let option = { "multi": true, "dataType": "array", "limit": 3000, 'batchSize': 3000 };
        if (bag) {
            query["bag"] = bag;
        }
        return this.count(query, option);
    }
    mpage(page, bag) {

        let $page = page || 1;
        let $size = 500;
        let $sort = '_id';
        let $order = 'desc';
        let dbsort = {};
        if ($sort) {
            dbsort[$sort] = $order == 'desc' ? -1 : 1;
        }
        let query = { "uid": this.uid };
        if (bag) {
            query["bag"] = bag;
        }
        let option = { "multi": true, "dataType": "array", "limit": 3000, 'batchSize': 3000 };
        return this.page(query, $page, $size, dbsort, option);

    }



}



module.exports = function (sid, uid) {
    return new item(sid, uid);
};

//索引
module.exports.indexes = [
    [{ "uid": 1, "bag": 1 }, {}]
]

function ObjectID(id) {
    if (!id) {
        throw new Error("model[item].ObjectID id empty");
    }
    return id;
}